package com.example.ktapplication.utils

import android.content.Context
import com.example.ktapplication.fragment.HomeFragment


class DpUtils {


    companion object {
        private var instance: DpUtils? = null

        fun getInstance(): DpUtils {
            if (instance == null) {
                instance = DpUtils()
            }
            return instance!!
        }
    }

    fun px2dp(context: Context, pxValue: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (pxValue / scale + 0.5f).toInt() // + 0.5f是为了让结果四舍五入
    }

    fun px2sp(context: Context, pxValue: Float): Int {
        val fontScale = context.resources.displayMetrics.scaledDensity
        return (pxValue / fontScale + 0.5f).toInt()
    }

}